<?php

namespace app\common\traits;

use app\common\utils\ErrorCode;
use think\Response;
use think\response\Json;

trait ResultData {

    protected function json(int $error, string $msg, mixed $data): Json {
        $code = 200;
        if ($error > 100 && $error < 1000) {
            $code = $error;
        }

        return Response::create([
            'error' => $error,
            'msg' => $msg,
            'data' => $data
        ], 'json', $code);

    }

    protected function success(mixed $data = [], string $msg = ErrorCode::SUCCESS[0], int $error = ErrorCode::SUCCESS[1]): Json {

        return $this->json(error: $error, msg: $msg, data: $data);
    }

    protected function error(string $msg = ErrorCode::FAIL[0], int $error = ErrorCode::FAIL[1], mixed $data = [],
                             array $errorCode = []): Json {
        if ($errorCode) {
            $error = $errorCode[1];
            $msg = $errorCode[0];
        }
        return $this->json(error: $error, msg: $msg, data: $data);
    }

}